package com.sta.staff.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sta.staff.bean.Result;
import com.sta.staff.mapper.UsersMapper;
import com.sta.staff.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.util.List;

@Service
public class RegService {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private BCryptPasswordEncoder encoder;
    //用户注册
    public Result register(Users users){
        if (users.getUsername()==null ||users.getUsername().length()<1){
            return new Result(false,"工号不能为空");
        }
        if (users.getName()==null || users.getName().length()<1){
            return new Result(false,"姓名不能为空");
        }
        if (users.getPhoneNum()==null || users.getPhoneNum().length()<1){
            return new Result(false,"手机号不能为空");
        }
        if (users.getPassword()==null || users.getPassword().length()<1){
            return new Result(false,"密码不能为空");
        }
        if (users.getPassword1()==null || users.getPassword1().length()<1){
            return new Result(false,"重复密码不能为空");
        }
        if (users.getAddress()==null || users.getAddress().length()<1){
            return new Result(false,"地址不能为空");
        }
        if (users.getSex()==null || users.getSex().length()<1){
            return new Result(false,"性别不能为空");
        }
        //验证工号是否重复
        QueryWrapper<Users> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",users.getUsername());
        List<Users> usersList = usersMapper.selectList(queryWrapper);
        if (usersList.size()>0){
            return new Result(false,"工号已存在");
        }
        //验证手机号是否重复
        QueryWrapper<Users> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("phoneNum",users.getPhoneNum());
        List<Users> usersList1 = usersMapper.selectList(queryWrapper1);
        if (usersList1.size()>0){
            return new Result(false,"手机号已存在");
        }


        //验证俩次密码是否一致
        if (!users.getPassword().equals(users.getPassword1())   ){
            return new Result(false,"俩次密码不一致");
        }
        // 加密密码
        String password = users.getPassword();
        password =encoder.encode(password);
        users.setPassword(password);
        usersMapper.insert(users);
        return new Result(true,"注册成功");
    }

}
